//package cn.gem.rainbow.cfg.restful;
//
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerExceptionResolver;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.ByteArrayOutputStream;
//
//
///**
// * 让view视图页面抛出500错误
// * 详细错误结果页，
// * 不再转到static/error/500页面，
// * 而是直接在视图里返回exception信息
// *  如 自定义异常处理!!! java.lang.IllegalStateException: Optional int parameter 'sub' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type. at
// *
// * 此方法需要额外配置 yml
// * # 出现错误时, 直接抛出异常
// * spring.mvc.throw-exception-if-no-handler-found=true
// * # 设置静态资源映射访问路径
// * spring.mvc.static-path-pattern=/statics/**
// *
// *https://mp.weixin.qq.com/s?__biz=MzU3MTAzNTMzMQ==&mid=2247484348&idx=1&sn=e9b36572c721418b097396b50319d140&chksm=fce71810cb9091063e810327e44f7ed07256188aecd352fa43f37e63e63dc64292b1a48b00cf&token=823367253&lang=zh_CN#rd
// * 本篇博文虽然也介绍了一种新的全局异常处理方式，实现效果和ControllerAdvice也差不多，但是并不推荐用这种方法, 原因如下
// * HandlerExceptionResolver的方式没有ControllerAdvice方式简介优雅
// * 官方提供的DefaultHandlerExceptionResolver已经非常强大了，基本上覆盖了http的各种状态码，我们自己再去定制的必要性不大
// */
//@Component
//public class SelfExceptionHandler implements HandlerExceptionResolver {
//    /**
//     * Try to resolve the given exception that got thrown during handler execution,
//     * returning a {@link ModelAndView} that represents a specific error page if appropriate.
//     * <p>The returned {@code ModelAndView} may be {@linkplain ModelAndView#isEmpty() empty}
//     * to indicate that the exception has been resolved successfully but that no view
//     * should be rendered, for instance by setting a status code.
//     *
//     * @param request  current HTTP request
//     * @param response current HTTP response
//     * @param handler  the executed handler, or {@code null} if none chosen at the
//     *                 time of the exception (for example, if multipart resolution failed)
//     * @param ex       the exception that got thrown during handler execution
//     * @return a corresponding {@code ModelAndView} to forward to,
//     * or {@code null} for default processing in the resolution chain
//     */
//    @Override
//    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//        String msg = SelfExceptionHandler.getThrowableStackInfo(ex);
//
//        try {
//            response.addHeader("Content-Type", "text/html; charset=UTF-8");
//            response.getWriter().append("自定义异常处理!!! \n").append(msg).flush();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    // 堆栈信息打印方法如下
//    public static String getThrowableStackInfo(Throwable e) {
//        ByteArrayOutputStream buf = new ByteArrayOutputStream();
//        e.printStackTrace(new java.io.PrintWriter(buf, true));
//        String msg = buf.toString();
//        try {
//            buf.close();
//        } catch (Exception t) {
//            return e.getMessage();
//        }
//        return msg;
//    }
//}
